package cn.wps.moffice.demo.menu;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import cn.wps.moffice.demo.R;
import cn.wps.moffice.demo.util.Define;
import cn.wps.moffice.demo.util.SettingPreference;

/**
 * @author ：chenyunbiao
 */
public class AlertDialogFragment extends DialogFragment {
	
	private final String[] WPS_OEPNMODE = 
		{
			Define.WPS_OPEN_AIDL,
			Define.WPS_OPEN_THIRD
		};
	
	private final String[] OPEN_MODE = 
		{
			Define.READ_ONLY, 
			Define.NORMAL, 
			Define.READ_MODE,
			Define.SAVE_ONLY,
			Define.EDIT_MODE
		};
	
	SettingPreference settingPreference;
	
	public static AlertDialogFragment newInstance(int type) {
		AlertDialogFragment frag = new AlertDialogFragment();
		Bundle bundle = new Bundle();
		bundle.putInt("type", type);
		frag.setArguments(bundle);
		return frag;
	}
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
//		View view;
		int type = getArguments().getInt("type");
		settingPreference = new SettingPreference(getActivity());
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
//		LayoutInflater inflater = getActivity().getLayoutInflater();
		switch (type) {
		case 1:
			int i = 0;  
			//获取之前的启动模式
        	String wps_openMode = settingPreference.getSettingParam(Define.WPS_OPEN_MODE, Define.WPS_OPEN_THIRD);
        	for (i = 0; i < WPS_OEPNMODE.length && !wps_openMode.equals(WPS_OEPNMODE[i]); i++);
			builder.setTitle("启动WPS模式选择");
			builder.setSingleChoiceItems(R.array.wpsmode, i, new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					settingPreference.setSettingParam(Define.WPS_OPEN_MODE, WPS_OEPNMODE[which]);
					dialog.dismiss();
				}
			});
			break;
		case 2:
			int j = 0;  
			//获取之前的打开模式
        	String openMode = settingPreference.getSettingParam(Define.OPEN_MODE, Define.NORMAL);
        	for (i = 0; i < WPS_OEPNMODE.length && !openMode.equals(OPEN_MODE[j]); j++);
			builder.setTitle("文档模式选择");
			builder.setSingleChoiceItems(R.array.readmode, j, new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					settingPreference.setSettingParam(Define.OPEN_MODE, OPEN_MODE[which]);
					dialog.dismiss();
				}
			});
			break;
		default:
			break;
		}
		return builder.create();
	}
}
